//
//  NSString+Color.m
//  Seeme
//
//  Created by Jiang on 2017/2/20.
//  Copyright © 2017年 Jiang. All rights reserved.
//

#import "NSString+Color.h"

@implementation NSString (Color)

/**
 给关键字标记不同颜色
 
 @param keyStrings 关键字
 @param keyColor 关键字颜色
 @param keyFont 关键字字体大小
 @param normalColor 普通文字颜色
 @param normalFont 普通文字字号
 @return 标记后的NSAttributedString
 */
- (NSMutableAttributedString *)rj_keyStrings:(NSArray<NSString *> *)keyStrings keyColor:(UIColor *)keyColor keyFont:(UIFont *)keyFont normalColor:(UIColor *)normalColor normalFont:(UIFont *)normalFont {
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self];
    
    NSDictionary *normalAttribute = @{NSFontAttributeName : normalFont,
                                      NSForegroundColorAttributeName : normalColor};
    NSDictionary *keyAttribute = @{NSFontAttributeName : keyFont,
                                   NSForegroundColorAttributeName : keyColor};
    
    [attributeString addAttributes:normalAttribute range:NSMakeRange(0, self.length)];
    
    for (NSString *str in keyStrings) {
        
        [attributeString addAttributes:keyAttribute range:[self rangeOfString:str]];
    }
    
    return attributeString;
}

- (NSAttributedString *)rj_keyString:(NSString *)keyString keyColor:(UIColor *)keyColor keyFont:(UIFont *)keyFont normalColor:(UIColor *)normalColor normalFont:(UIFont *)normalFont {
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self];
    
    NSDictionary *normalAttribute = @{NSFontAttributeName : normalFont,
                          NSForegroundColorAttributeName : normalColor};
    NSDictionary *keyAttribute = @{NSFontAttributeName : keyFont,
                          NSForegroundColorAttributeName : keyColor};
    
    [attributeString addAttributes:normalAttribute range:NSMakeRange(0, self.length)];
    [attributeString addAttributes:keyAttribute range:[self rangeOfString:keyString]];
    
    return attributeString;
}
@end
